﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using ECCIT.Extension.Weibo_v2.Model;
using ECCIT.Extension.Weibo_v2.Common;
using ECCIT;

using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.Mobile.Utilities;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Utility;

namespace OgilvyOne.UKNow.Mobile.APIs.Comments
{
    public class GetList : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string currpage = "0";
                if (context.Request.QueryString["current_page"] != null)
                {
                    if (Convert.ToInt32(context.Request.QueryString["current_page"]) > 0)
                        currpage = (Convert.ToInt32(context.Request.QueryString["current_page"]) - 1).ToString();
                }

                string pagesize = "10";
                if (context.Request.QueryString["page_size"] != null)
                    pagesize = context.Request.QueryString["page_size"];

                context.Response.Write(ResultUtility.ReturnResultJson("0", toGetMods(Convert.ToInt32(2052), Convert.ToInt32(currpage), Convert.ToInt32(pagesize), context), "获取评论列表成功"));
            }
            catch (Exception ex)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-1", "", "获取评论列表失败。 错误信息：" + ex.Message));
            }
        }

        private string toGetMods(int langid, int currengPageIndex, int pageSize, HttpContext htc)
        {
            int? uid = null;
            if (htc.Request.Params["uid"] != null)
                uid = Convert.ToInt32(htc.Request.Params["uid"]);

            EventsCommentBLL ecbll = new EventsCommentBLL(langid);
            PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currengPageIndex, pageSize, null);
            PagingList plst = null;

            string evtID = htc.Request.Params["eventid"];
            plst = string.IsNullOrEmpty(evtID) ? ecbll.getAllPulishedCommons(pagingParams, uid) : ecbll.GetEventsCommentModelsByEventsID(int.Parse(evtID), pagingParams);

            //ModelList<EventComment> comments = new ModelList<EventComment>(pagingParams, plst.Capacity);
            List<EventComment> list = new List<EventComment>();
            if (plst != null && plst.Capacity > 0)
            {
                foreach (EventsCommentModel ecm in plst.List)
                {
                    var comment = new EventComment();
                    comment.COL_EVENTS_ID = ecm.EventsID;
                    comment.COL_COMMENT_TIME = DateTimeFormatter.FormatDateTime(ecm.Time);
                    comment.COL_COMMENT_TEXT = ecm.Content;
                    comment.MEMBER_ID = ecm.MemberID;
                    comment.USERNAME = ecm.Member.Name;
                    comment.COL_EVENTS_TITLE = ecm.Events.Title;
                    comment.COL_COMMENT_IMG_NAME = "";
                    if (!string.IsNullOrEmpty(ecm.ImageName))
                        comment.COL_COMMENT_IMG_NAME = ConfigCacheManager.DomainName +
                                                       ConfigCacheManager.FilePath.GetFrontImageVirtualPath(
                                                           ecm.ImageName);

                    comment.Avatar = ConfigCacheManager.DomainName + ConfigCacheManager.FileName.DefaultUserHeadImage;
                    if (!string.IsNullOrEmpty(ecm.Member.MemberImg))
                        comment.Avatar = ConfigCacheManager.DomainName +
                                         ConfigCacheManager.FilePath.GetFrontImageVirtualPath(ecm.Member.MemberImg);

                    list.Add(comment);
                }
            }

            var pagelist = new PageOfItems<EventComment>();
            pagelist.PageIndex = currengPageIndex + 1;
            pagelist.PageSize = pageSize;
            pagelist.Data = list;
            pagelist.TotalItemCount = plst != null ? plst.Capacity : 0;
            pagelist.TotalPageCount = plst != null
                                          ? ((plst.Capacity % pagingParams.PageSize == 0)
                                                 ? plst.Capacity / pagingParams.PageSize
                                                 : plst.Capacity / pagingParams.PageSize + 1)
                                          : 0;

            return JsonSerializationHelper.ObjectToJson(pagelist);
        }
    }
}
